#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import shutil
import struct
import operator
import re
import sys

Android_SDK_TOOLS = ""

def readFile(filePath):
	file_object = open(filePath, 'rb')
	all_the_text = file_object.read()
	file_object.close()
	return all_the_text

def write2File(filename, data, wb):
	wf = open(filename, wb)
	wf.write(data)
	wf.close()

def makeUserConfig(cfgFile, userCfgFile):
	global Android_SDK_TOOLS
	curPath = os.getcwd()
	curPath = curPath.replace("\\","/")
	cfgs = readFile(cfgFile)
	cfgs = cfgs.decode('utf-8')
	cfgs = cfgs.replace("{$Android_SDK_TOOLS}", Android_SDK_TOOLS)
	cfgs = cfgs.replace("{$PWD}", curPath)
	cfgs = cfgs.encode('utf-8')
	write2File(userCfgFile, cfgs, "wb")
	print(u"生成 "+userCfgFile)
	
def main(android_sdk_tools):
	global Android_SDK_TOOLS
	
#	while os.path.isdir( "userconfig" ) == False:
#		android_sdk_tools = input(u"请输入Android SDK 下Tools的路径:")
#		if os.path.isfile( android_sdk_tools+"/android" ) == True:
#			Android_SDK_TOOLS = android_sdk_tools
#			break

	Android_SDK_TOOLS = android_sdk_tools
	
	if os.path.isdir( "userconfig" ) == False:
		os.makedirs("userconfig")
	else:
#		print(u"已设置。如果需要重新设置，请删除userconfig目录\n")
#		option = input(u"<1> 更新渠道信息\n")
#		if option == "1":
#			makeUserConfig("config/channelinfolist.conf", "userconfig/channelinfolist.conf")
#			return
#		else:
#			return
		shutil.rmtree( "userconfig" ,True)
		os.makedirs("userconfig")
		
	Android_SDK_TOOLS = Android_SDK_TOOLS.replace("\\","/")
	
	makeUserConfig("config/ant.properties", "userconfig/ant.properties")
	makeUserConfig("config/channelinfolist.conf", "userconfig/channelinfolist.conf")
	makeUserConfig("config/gameinfo.conf", "userconfig/gameinfo.conf")
	
	print(u"完成设置")

if __name__=="__main__":
	if(len(sys.argv) > 1):
		main(sys.argv[1])
	else:
		main("")
	
#	print( os.getcwd() )
